package com.ad.pet.common.data.utils;

import android.text.TextUtils;

import java.util.Collection;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by z on.
 * Des:
 * Date: 2017/3/10
 */

public class StringUtil {
    public static String join(Object[] array, String separator, int startIndex, int endIndex) {
        if (array == null) {
            return null;
        } else {
            int bufSize = endIndex - startIndex;
            if (bufSize <= 0) {
                return "";
            } else {
                bufSize *= (array[startIndex] == null ? 16 : array[startIndex].toString().length()) + 1;
                StringBuffer buf = new StringBuffer(bufSize);

                for (int i = startIndex; i < endIndex; ++i) {
                    if (i > startIndex) {
                        buf.append(separator);
                    }

                    if (array[i] != null) {
                        buf.append(array[i]);
                    }
                }
                return buf.toString();
            }
        }
    }

    public static String join(Object[] array, String separator) {
        return array == null ? null : join(array, separator, 0, array.length);
    }

    public static String join(Collection collection, String separator) {
        return collection == null ? null : join(collection.iterator(), separator);
    }

    public static String join(Iterator iterator, String separator) {
        if (iterator == null) {
            return null;
        } else if (!iterator.hasNext()) {
            return "";
        } else {
            Object first = iterator.next();
            if (!iterator.hasNext()) {
                return toString(first);
            } else {
                StringBuffer buf = new StringBuffer(256);
                if (first != null) {
                    buf.append(first);
                }

                while (iterator.hasNext()) {
                    if (separator != null) {
                        buf.append(separator);
                    }

                    Object obj = iterator.next();
                    if (obj != null) {
                        buf.append(obj);
                    }
                }

                return buf.toString();
            }
        }
    }

    public static String toString(Object obj) {
        return obj == null ? "" : obj.toString();
    }


    /**
     * 是否是数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    public static boolean isEmpty(String... strs) {
        for (String str : strs) {
            if (TextUtils.isEmpty(str)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isBlank(String str) {
        if (str == null) {
            return true;
        } else {
            int len = str.length();

            for (int i = 0; i < len; ++i) {
                if (!Character.isWhitespace(str.codePointAt(i))) {
                    return false;
                }
            }

            return true;
        }
    }

    public static boolean isNotBlank(String str) {
        if (str == null) {
            return false;
        } else {

            int len = str.length();

            for (int i = 0; i < len; ++i) {
                if (!Character.isWhitespace(str.codePointAt(i))) {
                    return true;
                }
            }

            return false;
        }
    }

    public static boolean isMessyCode(String strName) {
        try {
            Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
            Matcher m = p.matcher(strName);
            String after = m.replaceAll("");
            String temp = after.replaceAll("\\p{P}", "");
            char[] ch = temp.trim().toCharArray();

            int length = (ch != null) ? ch.length : 0;
            for (int i = 0; i < length; i++) {
                char c = ch[i];
                if (!Character.isLetterOrDigit(c)) {
                    String str = "" + ch[i];
                    if (!str.matches("[\u4e00-\u9fa5]+")) {
                        return true;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 数值小于10时在前面加上0
     *
     * @param num
     * @return
     */
    public static String getStringByNum(int num) {
        if (num < 10) {
            return "0" + num;
        }
        return String.valueOf(num);
    }

    /**
     * 两个值相比较 如果第一个是空的返回第二个
     *
     * @param first
     * @param last
     * @return
     */
    public static String firstEqualsLast(String first, String last) {
        if (TextUtils.isEmpty(first)) {
            if (TextUtils.isEmpty(last)) {
                return "";
            }
            return last;
        }
        return first;
    }


    /**
     * 返回 格式化好的size
     *
     * @param size
     * @return
     */
    public static String getStringBySize(long size) {
        if (size < 1024) {
            return size + " B";
        }
        if ((size /= 1024) < 1024) {
            return size + " KB";
        }
        double v = (size /= 1024.0);
        if (v < 1024.0) {
            return v + " MB";
        }
        return size / 1024 + " GB";
    }


}
